﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using bdxyit.com.RecruitmentAppSys.ViewModels;
using System.Diagnostics;

namespace bdxyit.com.RecruitmentAppSys.Controllers
{
    public class EducationInfoController : Controller
    {
        // GET: /EducationInfo/

        public ActionResult Index(string id)
        {
            return View();
        }

        //
        // GET: /EducationInfo/Details/5

        public ActionResult Details(string id)
        {
            var educationInfo = new EducationInfoViewModels();

            //educationInfo.UserEducation = rdb.UserEducations.SingleOrDefault(a => a.UserID == id && a.Flag == 0);
            //educationInfo.UserEducation2 = rdb.UserEducations.SingleOrDefault(a => a.UserID == id && a.Flag == 1);
            //educationInfo.AreaList = rdb.AreaLists.ToList();
            //educationInfo.SystemInfoEdu = rdb.SystemInfos.Where(a => a.Type == "EducationType").ToList();
            //educationInfo.SystemInfoLanguage = rdb.SystemInfos.Where(a => a.Type == "Languages").ToList();
            //educationInfo.SystemInfoComputer = rdb.SystemInfos.Where(a => a.Type == "computer").ToList();
            //educationInfo.UniversityList = rdb.UniversityLists.ToList();
            //educationInfo.ProfessionalTypeList = rdb.ProfessionalTypeLists.ToList();
            //educationInfo.ProfessionalList = rdb.ProfessionalLists.ToList();
            //educationInfo.DegreeList = rdb.DegreeLists.ToList();
            //educationInfo.EducationList = rdb.EducationLists.ToList();
            //educationInfo.AbilityLevelLang1 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 1);
            //educationInfo.AbilityLevelLang2 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 2);
            //educationInfo.AbilityLevelLang3 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 3);
            //educationInfo.AbilityLevelCom = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "computer" && a.Flag == 1);
            //educationInfo.LanguageLevel = rdb.LanguageLevels.ToList();
            //educationInfo.ComputerLevel = rdb.ComputerLevels.ToList(); 
            //educationInfo.AbilityLevel = rdb.AbilityLevels.Where(a => a.UserID == id ).ToList();
            return View(educationInfo);
        }

        //
        // GET: /EducationInfo/Create

        public ActionResult Create(string id)
        {
            var educationInfo = new EducationInfoViewModels();
            //educationInfo.UserEducation = new UserEducation();
            //educationInfo.UserEducation2 = new UserEducation();
           
            //educationInfo.SystemInfoEdu = rdb.SystemInfos.Where(a => a.Type == "EducationType").ToList();
            //educationInfo.SystemInfoLanguage = rdb.SystemInfos.Where(a => a.Type == "Languages").ToList();
            //educationInfo.SystemInfoComputer = rdb.SystemInfos.Where(a => a.Type == "computer").ToList();

            //educationInfo.AreaList = rdb.AreaLists.ToList();
            //educationInfo.UniversityList = rdb.UniversityLists.ToList();
            //educationInfo.ProfessionalTypeList = rdb.ProfessionalTypeLists.ToList();
            //educationInfo.ProfessionalList = rdb.ProfessionalLists.ToList();
            //educationInfo.DegreeList = rdb.DegreeLists.ToList();
            //educationInfo.EducationList = rdb.EducationLists.ToList();
            //educationInfo.LanguageLevel = rdb.LanguageLevels.ToList();
            //educationInfo.ComputerLevel = rdb.ComputerLevels.ToList(); 
            //educationInfo.UserEducation.UserID = id;
           
            //educationInfo.UserEducation.StartTime =DateTime.Parse(DateTime.Now.ToShortDateString());
            //educationInfo.UserEducation.EndTime = DateTime.Parse(DateTime.Now.ToShortDateString());
            //educationInfo.UserEducation2.StartTime = DateTime.Parse(DateTime.Now.ToShortDateString());
            //educationInfo.UserEducation2.EndTime = DateTime.Parse(DateTime.Now.ToShortDateString());
            //try
            //{
            //    var ue = rdb.UserEducations.Where(a => a.UserID == id).ToList();
            //    if (ue != null)
            //    {
            //        foreach (var u in ue)
            //        {
            //            rdb.UserEducations.Remove(u);
            //        }
            //    }
            //    var al = rdb.AbilityLevels.Where(a => a.UserID == id).ToList();
            //    if (al != null)
            //    {
            //        foreach (var a in al)
            //        {
            //            rdb.AbilityLevels.Remove(a);
            //        }
            //    }
            //    rdb.SaveChanges();
            //}
            //catch 
            //{
            //    return RedirectToAction("Index");
            //}
            return View(educationInfo);
        } 

        //
        // POST: /EducationInfo/Create

        [HttpPost]
        public ActionResult Create(EducationInfoViewModels EducationInfo,FormCollection form)
        {
            //string[] sys = form["SysID"].Split(new char[] { ',' });

            //string[] University = form["UniversityID"].Split(new char[] { ',' });

            //string[] Degree = form["DegreeID"].Split(new char[] { ',' });

            //string[] Pro = form["ProID"].Split(new char[]{','});

            //string[] Edu = form["EduID"].Split(new char[]{','});

            //string[] Level = form["LevelID"].Split(new char[]{','});

            //try
            //{
            //    EducationInfo.UserEducation.UserID = form["UserEducation.UserID"].ToString();//教育类型
            //    EducationInfo.UserEducation2.UserID = form["UserEducation.UserID"].ToString();//教育类型
                

            //    EducationInfo.UserEducation.SysId = sys[0].ToString();//教育类型
            //    EducationInfo.UserEducation2.SysId = sys[1].ToString();//教育类型

            //    EducationInfo.UserEducation.UniversityID = University[0].ToString();//大学
            //    EducationInfo.UserEducation2.UniversityID = University[1].ToString();//大学

            //    EducationInfo.UserEducation.DegreeID = Degree[0].ToString();//学历
            //    EducationInfo.UserEducation2.DegreeID = Degree[1].ToString();//学历

            //    EducationInfo.UserEducation.PorID = Pro[0].ToString();//专业
            //    EducationInfo.UserEducation2.PorID = Pro[1].ToString();//专业

            //    EducationInfo.UserEducation.EduID = Edu[0].ToString();//学位
            //    EducationInfo.UserEducation2.EduID = Edu[1].ToString();//学位


            //    EducationInfo.AbilityLevelLang1.UserID = form["UserEducation.UserID"].ToString();
            //    EducationInfo.AbilityLevelLang2.UserID = form["UserEducation.UserID"].ToString();
            //    EducationInfo.AbilityLevelLang3.UserID = form["UserEducation.UserID"].ToString();
            //    EducationInfo.AbilityLevelCom.UserID = form["UserEducation.UserID"].ToString();
            //    EducationInfo.AbilityLevelLang1.Type = "Languages";
            //    EducationInfo.AbilityLevelLang2.Type = "Languages";
            //    EducationInfo.AbilityLevelLang3.Type = "Languages";
            //    EducationInfo.AbilityLevelCom.Type = "computer";

            //    EducationInfo.AbilityLevelLang1.SysID = sys[2].ToString();//语言种类
            //    EducationInfo.AbilityLevelLang1.LevelID = Level[0].ToString();//语言等级

            //     EducationInfo.AbilityLevelLang2.SysID = sys[3].ToString();//语言种类;
            //     EducationInfo.AbilityLevelLang2.LevelID = Level[1].ToString();//语言等级

            //     EducationInfo.AbilityLevelLang3 .SysID= sys[4].ToString();//语言种类
            //     EducationInfo.AbilityLevelLang3.LevelID = Level[2].ToString();//语言等级

            //     EducationInfo.AbilityLevelCom.SysID =sys[5].ToString();//语言种类
            //     EducationInfo.AbilityLevelCom.LevelID = Level[3].ToString();//语言等级

            //     EducationInfo.UserEducation.Flag = 1;
            //     EducationInfo.UserEducation2.Flag = 2;
            //     EducationInfo.AbilityLevelLang1.Flag = 1;
            //     EducationInfo.AbilityLevelLang2.Flag = 2;
            //     EducationInfo.AbilityLevelLang3.Flag = 3;
            //     EducationInfo.AbilityLevelCom.Flag = 1;
                 

            //    // TODO: Add insert logic here
            //    if (ModelState.IsValid)
            //    {
            //        rdb.UserEducations.Add(EducationInfo.UserEducation);
            //        rdb.UserEducations.Add(EducationInfo.UserEducation2);
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelCom);
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang1);
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang2);
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang3);
            //        rdb.SaveChanges();
            //    }
            //    return RedirectToAction("Create", "WorkHistory", new
            //    {
            //        id = EducationInfo.UserEducation.UserID
            //    });
            //}
            //catch
            //{
            //    return RedirectToAction("Index");
            //}
            return RedirectToAction("Index");
        }
        
        //
        // GET: /EducationInfo/Edit/5
 
        public ActionResult Edit(string id)
        {
            //id = "12312312";
            var educationInfo = new EducationInfoViewModels();
            //educationInfo.UserEducation = new UserEducation();
            //educationInfo.UserEducation.UserID = id;
            //educationInfo.UserEducation = rdb.UserEducations.SingleOrDefault(a => a.UserID == id && a.Flag == 1);
            //educationInfo.UserEducation2 = rdb.UserEducations.SingleOrDefault(a => a.UserID == id && a.Flag == 2);
            //educationInfo.AreaList = rdb.AreaLists.ToList();
            //educationInfo.SystemInfoEdu = rdb.SystemInfos.Where(a => a.Type == "EducationType").ToList();
            //educationInfo.SystemInfoLanguage = rdb.SystemInfos.Where(a => a.Type == "Languages").ToList();
            //educationInfo.SystemInfoComputer = rdb.SystemInfos.Where(a => a.Type == "computer").ToList();
            //educationInfo.UniversityList = rdb.UniversityLists.ToList();
            //educationInfo.ProfessionalTypeList = rdb.ProfessionalTypeLists.ToList();
            //educationInfo.ProfessionalList = rdb.ProfessionalLists.ToList();
            //educationInfo.DegreeList = rdb.DegreeLists.ToList();
            //educationInfo.EducationList = rdb.EducationLists.ToList();
            //educationInfo.LanguageLevel = rdb.LanguageLevels.ToList();
            //educationInfo.ComputerLevel = rdb.ComputerLevels.ToList(); 
            //educationInfo.AbilityLevelLang1 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 1);
            //educationInfo.AbilityLevelLang2 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 2);
            //educationInfo.AbilityLevelLang3 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "Languages" && a.Flag == 3);
            //educationInfo.AbilityLevelCom = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == id && a.Type == "computer" && a.Flag == 1);
            return View(educationInfo);
        }

        //
        // POST: /EducationInfo/Edit/5

        [HttpPost]
        public ActionResult Edit(EducationInfoViewModels EducationInfo, FormCollection form)
        {
            string[] sys = form["SysID"].Split(new char[] { ',' });

            string[] University = form["UniversityID"].Split(new char[] { ',' });

            string[] Degree = form["DegreeID"].Split(new char[] { ',' });

            string[] Pro = form["ProID"].Split(new char[] { ',' });

            string[] Edu = form["EduID"].Split(new char[] { ',' });

            string[] Level = form["LevelID"].Split(new char[] { ',' });

            string strID = form["UserEducation.UserID"].ToString();
            //try
            //{
                // TODO: Add update logic here

            //    var edf = rdb.UserEducations.SingleOrDefault(a => a.UserID == strID && a.Flag == 1);
            //    if (edf != null) 
            //    {
            //        rdb.UserEducations.Remove(edf);
            //        EducationInfo.UserEducation.SysId = sys[0].ToString();
            //        EducationInfo.UserEducation.UniversityID = University[0].ToString();
            //        EducationInfo.UserEducation.PorID = Pro[0].ToString();
            //        EducationInfo.UserEducation.DegreeID = Degree[0].ToString();
            //        EducationInfo.UserEducation.EduID = Edu[0].ToString();
            //        EducationInfo.UserEducation.Flag = 1;
            //        EducationInfo.UserEducation.UserID = strID;
            //        rdb.UserEducations.Add(EducationInfo.UserEducation);
            //    }
            //    var edh = rdb.UserEducations.SingleOrDefault(a => a.UserID == strID && a.Flag == 2);
            //    if (edh != null)
            //    {
            //        rdb.UserEducations.Remove(edh);
            //        EducationInfo.UserEducation2.SysId = sys[1].ToString();
            //        EducationInfo.UserEducation2.UniversityID = University[1].ToString();
            //        EducationInfo.UserEducation2.PorID = Pro[1].ToString();
            //        EducationInfo.UserEducation2.DegreeID = Degree[1].ToString();
            //        EducationInfo.UserEducation2.EduID = Edu[1].ToString();
            //        EducationInfo.UserEducation2.Flag = 2;
            //        EducationInfo.UserEducation2.UserID = strID;
            //        rdb.UserEducations.Add(EducationInfo.UserEducation2);
            //        //rdb.SaveChanges();
            //    }

            //    var all1 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == strID && a.Flag == 1 && a.Type == "Languages");
            //    if (all1 != null) 
            //    {
            //        rdb.AbilityLevels.Remove(all1);
            //        EducationInfo.AbilityLevelLang1.SysID = sys[2].ToString();
            //        EducationInfo.AbilityLevelLang1.LevelID = Level[0].ToString();
            //        EducationInfo.AbilityLevelLang1.Type = "Languages";
            //        EducationInfo.AbilityLevelLang1.UserID = strID;
            //        EducationInfo.AbilityLevelLang1.Flag = 1;
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang1);
            //    }
            //    var all2 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == strID && a.Flag == 2 && a.Type == "Languages");
            //    if (all2 != null)
            //    {
            //        rdb.AbilityLevels.Remove(all2);
            //        EducationInfo.AbilityLevelLang2.SysID = sys[3].ToString();
            //        EducationInfo.AbilityLevelLang2.LevelID = Level[1].ToString();
            //        EducationInfo.AbilityLevelLang2.Type = "Languages";
            //        EducationInfo.AbilityLevelLang2.UserID = strID;
            //        EducationInfo.AbilityLevelLang2.Flag = 2;
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang2);
            //    }
            //    var all3 = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == strID && a.Flag == 3 && a.Type == "Languages");
            //    if (all3 != null)
            //    {
            //        rdb.AbilityLevels.Remove(all3);
            //        EducationInfo.AbilityLevelLang3.SysID = sys[4].ToString();
            //        EducationInfo.AbilityLevelLang3.LevelID = Level[2].ToString();
            //        EducationInfo.AbilityLevelLang3.Type = "Languages";
            //        EducationInfo.AbilityLevelLang3.UserID = strID;
            //        EducationInfo.AbilityLevelLang3.Flag = 3;
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelLang3);
            //    }
            //    var alc = rdb.AbilityLevels.SingleOrDefault(a => a.UserID == strID && a.Type == "computer");
            //    if (alc != null)
            //    {
            //        rdb.AbilityLevels.Remove(alc);
            //        EducationInfo.AbilityLevelCom.SysID = sys[5].ToString();
            //        EducationInfo.AbilityLevelCom.LevelID = Level[3].ToString();
            //        EducationInfo.AbilityLevelCom.Type = "computer";
            //        EducationInfo.AbilityLevelCom.UserID = strID;
            //        EducationInfo.AbilityLevelCom.Flag = 1;
            //        rdb.AbilityLevels.Add(EducationInfo.AbilityLevelCom);
            //    }
            //    rdb.SaveChanges();
            //    return RedirectToAction("Create", "WorkHistory", new
            //    {
            //        id = EducationInfo.UserEducation.UserID
            //    });
            //}
            //catch
            //{
            //    return RedirectToAction("Index");
            //}
            return View();
        }

        //
        // GET: /EducationInfo/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /EducationInfo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, EducationInfoViewModels EducationInfo)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
